import { createWebHashHistory, createRouter } from 'vue-router'
import { checkLogin } from '@/utils/user'

const routes = [
  { 
    path: '/', 
    component: () => import('@/views/layout/index.vue'),
    redirect: '/home',
    meta: {
      usedPermission: true
    },
    children: [
      {
        path: '/home',
        name: '首页',
        meta: {
          id: 1,
          isShow: true
        },
        icon: 'http://120.48.166.114:9000/images/2024/10/30/c3c9df8a.png',
        component: () => import('@/views/pages/home/index.vue'),
      },
      {
        path: '/mine',
        name: '我的',
        meta: {
          id: 2,
          isShow: true
        },
        icon: 'http://120.48.166.114:9000/images/2024/10/30/56345702.png',
        component: () => import('@/views/pages/mine/index.vue'),
      },
      {
        path: '/test',
        name: '测试',
        icon: 'http://120.48.166.114:9000/images/2024/10/30/8434b4d2.png',
        meta: {
          id: 3,
          isShow: false
        },
        redirect: '/test/test1',
        component: () => import('@/views/pages/test/index.vue'),
        children: [
          {
            path: '/test/test1',
            name: '测试1',
            icon: '',
            meta: {
              fatherId: 3
            },
            component: () => import('@/views/pages/test/test01/index.vue')
          },
          {
            path: '/test/test2',
            name: '测试2',
            icon: '',
            meta: {
              fatherId: 3
            },
            component: () => import('@/views/pages/test/test02/index.vue')
          },
        ]
      }
    ]
  },{
    path: '/login',
    name: '登录',
    component: () => import('@/views/login/index.vue'),
  },{
    path: '/*',
    name: '页面丢失',
    component: () => import('@/views/404/index.vue'),
  }
]




const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 路由拦截
router.beforeEach((to, from, next) => {
  // 如果未登录，则跳转到登录页面
  if (!checkLogin() && to.path!== '/login') {
    next('/login?redirect=' + to.path)
  }else {
    next()
  }
})


export default router